﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Code.ProductType;
using E2_WEBSITE.Code.Admin;
using E2_WEBSITE.Code.Product;
using E2_WEBSITE.Code;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int iDaDangNhap = (Int32)Session["IsLogin"];
        if (iDaDangNhap == 1)
        {
            if ((Int32)Session["Authentication"] == 2) // user
            {
                lbUser.Text = (String)Session["Username"];
                hlLogin.Text = "Đăng xuất";
                hlLogin.NavigateUrl = "/Common/Logout.aspx";

                hlSpecial.Text = "Quản lý người dùng";
                hlSpecial.NavigateUrl = "/cp/member/member.aspx";
                hlSpecial.Visible = true;
            }
            else if ((Int32)Session["Authentication"] == 1) // employee
            {
                lbUser.Text = (String)Session["Username"];
                hlLogin.Text = "Đăng xuất";
                hlLogin.NavigateUrl = "/Common/Logout.aspx";

                hlSpecial.Text = "Trang web nhân viên";
                hlSpecial.NavigateUrl = "/cp/employee/employee.aspx";
                hlSpecial.Visible = true;
            }
            else if ((Int32)Session["Authentication"] == 0) // admin
            {
                lbUser.Text = (String)Session["Username"];
                hlLogin.Text = "Đăng xuất";
                hlLogin.NavigateUrl = "/Common/Logout.aspx";

                hlSpecial.Text = "Trang web quản trị";
                hlSpecial.NavigateUrl = "/cp/admin/admin.aspx";
                hlSpecial.Visible = true;
            }
            else
            {
                hlSpecial.Visible = false;
            }
        }
        else
        {
            hlSpecial.Visible = false;
        }
        Page.LoadComplete += new EventHandler(Page_LoadComplete);
    }

    void Page_LoadComplete(object sender, EventArgs e)
    {
        ProductTypeController pcl = new ProductTypeController();
        List<string> str = pcl.GetListProductTypeName();
        cbProductType.Items.Clear();
        cbProductType.Items.Add("-Chọn loại sản phẩm-");
        foreach (string s in str)
        {
            cbProductType.Items.Add(s);
        }
        cbBeginPrice.Items.Clear();
        cbBeginPrice.Items.Add("-Giá từ-");
        cbBeginPrice.Items.Add("1 triệu");
        cbBeginPrice.Items.Add("2 triệu");
        cbBeginPrice.Items.Add("3 triệu");
        cbBeginPrice.Items.Add("4 triệu");
        cbBeginPrice.Items.Add(">5 triệu");

        cbEndPrice.Items.Clear();
        cbEndPrice.Items.Add("-Giá đến-");
        cbEndPrice.Items.Add("1 triệu");
        cbEndPrice.Items.Add("2 triệu");
        cbEndPrice.Items.Add("3 triệu");
        cbEndPrice.Items.Add("4 triệu");
        cbEndPrice.Items.Add(">5 triệu");



        //load baner for ads
        AdsController adsController = new AdsController();
        List<AdsInfo> lstAds = adsController.GetLstAds(DateTime.Now, DateTime.Now);
        if (lstAds != null)
        {
            foreach (AdsInfo ads in lstAds)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                HyperLink hyperLink = new HyperLink();
                hyperLink.ImageUrl = "/images/Ads/" + ads.Image;
                hyperLink.NavigateUrl = ads.Navigate;

                cell.Controls.Add(hyperLink);
                row.Cells.Add(cell);
                this.tableAds.Rows.Add(row);
            }
        }

        
        ProductController productController = new ProductController();
        ProductInfo laptopInfo = productController.GetTopLaptop();
        if (laptopInfo != null)
        {
            //load sp header
            lSPHeaderName.Text = laptopInfo.TenSanPham;
            lSPHeaderDescription.Text = laptopInfo.TenSanPham + "với kiểu dáng trang nhả. Thiết kế gọn nhẹ, cùng với cấu hình cao giúp bạn có những trải nghiệm mới trong cuộc sống số";
            hlinkSPHeader.NavigateUrl = "/Products/detail.aspx?ProductId=" + laptopInfo.ID;
            lLaptopImg.ImageUrl = laptopInfo.Hinh_Anh.URL;

            //load sp hot
            hLinkSPHot.Text = laptopInfo.TenSanPham;
            hLinkSPHot.NavigateUrl = "/Products/detail.aspx?ProductId=" + laptopInfo.ID;
            hLinkSPHotImg.ImageUrl = laptopInfo.Hinh_Anh.URL;
            hLinkSPHotImg.PostBackUrl = "/Products/detail.aspx?ProductId=" + laptopInfo.ID;
            lReduce.Text = Utilities.ConvertToMoneyFormat(laptopInfo.Gia + laptopInfo.Gia * 20 / 100) + " VNĐ";
            lPrice.Text = Utilities.ConvertToMoneyFormat(laptopInfo.Gia) + " VNĐ";
        }               
    }

    protected void search_bt_Click(object sender, ImageClickEventArgs e)
    {
        //if (tbSearch.Text == "")
            return;
        //string str = tbSearch.Text;
       // Response.Redirect("/Search/advance-search.aspx?SearchCon=" + str);
    }

    protected void btnDoSearch_Click(object sender, EventArgs e)
    {
        string strSearchCon = tbSearch.Text;
        if(strSearchCon != "")
        {
            Response.Redirect("/Search/advance-search.aspx?SearchCon=" + strSearchCon);
        }
        else
        {
            if(cbProductType.SelectedIndex == 0)
            {
                Response.Redirect("/Search/advance-search.aspx");
            }
            else
            {
                string sProductType = cbProductType.Text;
                int iBeginPrice = cbBeginPrice.SelectedIndex;
                int iEndPrice = cbEndPrice.SelectedIndex;
                Response.Redirect("/Search/advance-search.aspx?ProType=" + sProductType + "&BegP=" + iBeginPrice.ToString() + "&EndP=" + iEndPrice.ToString());
            }
        }
    }
}